package com.ideal.manage.esms.util;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.WorkbookUtil;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class FileOperation {
    /**
     * 生成文件
     *
     * @param fileName
     */
    public static boolean createFile(File fileName) {
        if (!fileName.exists()) {
            try {
                fileName.createNewFile();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return true;
    }

    /**
     * @throws IOException
     * @description 读文件
     */
    public static String readTxtFile(MultipartFile fileName) throws IOException {
        String result = null;
        InputStream inputStream = fileName.getInputStream();
        InputStreamReader isr = new InputStreamReader(inputStream);
        BufferedReader reader = new BufferedReader(isr);

        String read = null;
        int count = 0;
        while ((read = reader.readLine()) != null) {
            result = result + count + read + "\r\n";
            count++;
        }

        if (reader != null) {
            reader.close();
        }

        if (reader != null) {
            reader.close();
        }

        System.out.println("¶ÁÈ¡ÎÄ¼þµÄÄÚÈÝÊÇ£º " + "\r\n" + result);
        return result;
    }

    /**
     * @throws IOException
     * @description 读文件
     */
    public static <T> List<T> readExcelFile(MultipartFile fileName, Class<T> dataClass) throws IOException, IllegalAccessException, InstantiationException {
        String result = null;
        InputStream inputStream = fileName.getInputStream();
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook(inputStream);
        List<T> listData = new LinkedList<T>();

        HSSFSheet sheetAt = hssfWorkbook.getSheetAt(0);
        int rowCount = sheetAt.getPhysicalNumberOfRows(); //获取总行数

        List<String> fieldNameList = new ArrayList<String>();
        T rowData = null;
        //遍历每一行
        for (int r = 0; r < rowCount; r++) {
            Row row = sheetAt.getRow(r);
            int cellCount = row.getPhysicalNumberOfCells(); //获取总列数

            if(r>0) {
                rowData = dataClass.newInstance();
                listData.add(rowData);
            }
            //遍历每一列
            for (int c = 0; c < cellCount; c++) {
                Cell cell = row.getCell(c);
                String stringCellValue = cell.getStringCellValue();
                if (r == 0) {
                    fieldNameList.add(stringCellValue);
                } else {
                    try {
                        if(c==0 || c ==4){
                            //第一列ID和最后一列newtime忽略
                            continue;
                        }
                        BeanUtils.copyProperty(rowData, fieldNameList.get(c), stringCellValue);
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }

                }
            }
        }
        return listData;
    }

    /**
     * @throws UnsupportedEncodingException
     * @throws IOException
     * @description 写文件
     */
    public static boolean writeTxtFile(String content, File fileName) throws UnsupportedEncodingException, IOException {
        FileOutputStream o = null;
        o = new FileOutputStream(fileName);
        o.write(content.getBytes("UTF-8"));
        o.close();
        return true;
    }

    /**
     * @throws IOException
     * @description 单元测试
     */
    public static void main(String[] args) throws IOException {
//    File file = new File("d:/hello.txt");
//    Date date = new Date();
//    SimpleDateFormat day = new SimpleDateFormat("yyyyMMddHHmmss");
//    String dateName = day.format(date);
//    File copyFile = new File("d:/" + dateName + ".txt");
//    String content = readTxtFile(file);
//    createFile(copyFile);
//    writeTxtFile(content,copyFile);
    }
}